﻿<UserControl x:Class="ResourceBlender_Express.DatabaseSelector"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:validators="clr-namespace:ResourceBlender_Express.Validators"
    MinWidth="270" MinHeight="250">
    <Grid DataContext="{Binding}" Loaded="DatabaseSelector_Loaded">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <Label Grid.Row="0" Grid.Column="0">Database Mode</Label>
        <ComboBox Grid.Row="0" Grid.Column="1" Margin="2" x:Name="cboDBMode" SelectedValue="{Binding Path=Default.DBMode}" SelectedValuePath="Tag">
            <ComboBoxItem Tag="0">MySQL</ComboBoxItem>
            <ComboBoxItem Tag="1">SQL Server</ComboBoxItem>
            <ComboBoxItem Tag="2">SQLite</ComboBoxItem>
        </ComboBox>

        <GroupBox Header="Connection Details" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2">
            <TabControl x:Name="tbcModeFields" Background="Transparent" BorderBrush="Transparent">
                <TabItem Visibility="Hidden" Height="0">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Label Grid.Row="0" Grid.Column="0" Margin="0,0,8,2">Server</Label>
                        <TextBox Grid.Row="0" Grid.Column="1" Margin="2,0,2,2" x:Name="txtDBHost">
                            <Binding Path="Default.DBHost" Mode="TwoWay">
                                <Binding.ValidationRules>
                                    <validators:StringValidationRule
                                    MinimumLength="0"
                                    MaximumLength="255"
                                    ErrorMessage="Database hostname must be entered" />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox>

                        <Label Grid.Row="1" Grid.Column="0">Database Port</Label>
                        <TextBox Grid.Row="1" Grid.Column="1" x:Name="txtDBPort">
                            <Binding Path="Default.DBPort" Mode="TwoWay">
                                <Binding.ValidationRules>
                                    <validators:IntegerRangeValidationRule
                                    MinValue="1"
                                    MaxValue="65536"
                                    IsGeneric="False"
                                    ErrorMessage="Database port must be between 1 and 65536" />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox>

                        <Label Grid.Row="2" Grid.Column="0">Username</Label>
                        <TextBox Grid.Row="2" Grid.Column="1" x:Name="txtDBUser">
                            <Binding Path="Default.DBUser" Mode="TwoWay">
                                <Binding.ValidationRules>
                                    <validators:StringValidationRule
                                    MinimumLength="0"
                                    MaximumLength="255"
                                    ErrorMessage="Database username must be entered" />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox>

                        <Label Grid.Row="3" Grid.Column="0">Password</Label>
                        <TextBox Grid.Row="3" Grid.Column="1" x:Name="txtDBPassword" Text="{Binding Path=Default.DBPassword,Mode=TwoWay}" />

                        <Label Grid.Row="4" Grid.Column="0">Database Name</Label>
                        <TextBox Grid.Row="4" Grid.Column="1" x:Name="txtDBDatabase">
                            <Binding Path="Default.DBDatabase" Mode="TwoWay">
                                <Binding.ValidationRules>
                                    <validators:StringValidationRule
                                    MinimumLength="0"
                                    MaximumLength="255"
                                    ErrorMessage="Database name must be entered" />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox>
                    </Grid>
                </TabItem>
                <TabItem Visibility="Hidden" Height="0">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        
                        <Label Grid.Row="0" Grid.Column="0">Database Path</Label>
                        <TextBox Grid.Row="0" Grid.Column="1" x:Name="txtDBPath">
                            <Binding Path="Default.DBPath" Mode="TwoWay">
                                <Binding.ValidationRules>
                                    <validators:StringValidationRule
                                        MinimumLength="4"
                                        ErrorMessage="If using SQLite, a database file is required" />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox>
                        <Button Grid.Row="1" Grid.Column="1" MinWidth="70" Click="BrowseForDBFile" HorizontalAlignment="Right">Browse...</Button>
                    </Grid>
                </TabItem>
            </TabControl>
        </GroupBox>
        <Label Grid.Row="2" Grid.Column="0">Table Prefix</Label>
        <TextBox Grid.Row="2" Grid.Column="1" x:Name="txtDBTablePrefix" Text="{Binding Path=Default.DBTablePrefix,Mode=TwoWay}" />
    </Grid>
</UserControl>
